package org.example.proxy;

import com.google.common.base.CaseFormat;
import org.apache.commons.lang.StringEscapeUtils;
import org.example.annotation.NotCamel;
import org.example.init.Context;
import org.example.utils.StrUtils;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import org.example.annotation.Type;
import org.example.utils.StrategyMethodUtil;

public class ProxyMethod implements InvocationHandler {
    private final  Object target;

    private final ReentrantLock lock=new ReentrantLock();

    public ProxyMethod(Object target) {
        this.target = target;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
            AtomicInteger counter= new AtomicInteger();
            for(Parameter p : method.getParameters())
            {
               if(null== args[counter.get()]|StrUtils.blank(args[counter.get()])) throw new RuntimeException("not null !!!");

                args[counter.get()]= StrategyMethodUtil.execute(p,args[counter.get()],target);
                counter.getAndIncrement();
            }
            try {
                 lock.lock();
                 return method.invoke(Object.class.equals(method.getDeclaringClass()) ? this : target, args);

            } finally {
                 lock.unlock();
             }



    }



}
